/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} n
 * @return {ListNode}
 */
 var removeNthFromEnd = function(head, n) {
  let cur = head
  while(n-- && cur){ // 这样写 就是n--算完之后才进入cur
    console.log(n);
    cur = cur.next
  }
  if(cur === null && n !== 0){
    // 说明还没到倒数n个 节点就没了
    return head
  }
  if(cur === null && n === 0){
    // 删除头结点
    return head.next
  }
  let ret = head
  while(cur.next){
    cur = cur.next
    ret = ret.next
  }
  ret.next = ret.next.next
  return head
};